package com.hzp.micromall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.common.core.api.CommonResult;
import com.hzp.micromall.product.dto.PmsBrandParam;
import com.hzp.micromall.product.entity.PmsBrand;
import com.hzp.micromall.product.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/26 14:32
 * @Version 1.0
 * 品牌功能Controller
 */
@Controller
@Api(tags = "商品品牌管理")
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService brandService;

    @ApiOperation(value = "添加品牌")
    @RequestMapping(value = "/createBrand", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBrandById(@Validated @RequestBody PmsBrandParam pmsBrand) {
        int count = brandService.createBrand(pmsBrand);
        if (count == 1) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "删除品牌")
    @RequestMapping(value = "/deleteBrandById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrandById(@PathVariable("id") Long id) {
        int count = brandService.deleteBrand(id);
        if (count == 1) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "批量删除品牌")
    @RequestMapping(value = "/deleteBrand/batch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBrandBatch(@RequestParam("ids") List<Long> ids) {
        int count = brandService.deleteBrand(ids);
        if (count > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "更新品牌")
    @RequestMapping(value = "/updateBrand/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id,
                               @Validated @RequestBody PmsBrandParam pmsBrandParam) {
        int count = brandService.updateBrand(id, pmsBrandParam);
        if (count == 1) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "批量更新显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids,
                                         @RequestParam("status") Integer status) {
        int count = brandService.updateShowStatus(ids, status);
        if (count > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "根据品牌名称分页获取品牌列表")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getBrandPageList(@RequestParam(value = "keyword", required = false) String keyword,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        CommonPage<PmsBrand> brandPage = brandService.getPage(keyword, pageNum, pageSize);
        return CommonResult.success(brandPage);
    }

    @ApiOperation(value = "根据编号查询品牌信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> getDetail(@PathVariable("id") Long id) {
        return CommonResult.success(brandService.getBrand(id));
    }

    @ApiOperation(value = "获取全部品牌列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> getList() {
        return CommonResult.success(brandService.listAllBrand());
    }

}
